$(document).ready(function() {
		$('#twoStepPreBtn').click(function() {
			$('.reservation2').hide();
			$('.reservation1').fadeIn(1000);
			$('.doctorSelect').empty();
		});
		$('#threeStepPreBtn').click(function() {
			$('.reservation3').hide();
			$('.reservation2').fadeIn(1000);
			$('.reservation3Div1').empty();
			$('.reservation3Div2').empty();
			$('.reservation3Div3').empty();
		});
		
		
		$('input:image').click(function() {
			var dept_id = $(this).attr('value');
			$.ajax({
				url : 'doctorProfile.do',
				data : 'dept_id=' + dept_id,
				dataType : "json",
				success : function(data) {
					$('.reservation1').hide();
					$('.reservation2').fadeIn(1000);
						for (var i = 0; i < data.doctorlist.length; i++) {	
							var html = "";
							html +="<table class='doctorSelect'>";
							html +="<tr>";
							html +="<td>";
							html +="<table class='doctorSelectTable' id='" + data.doctorlist[i].doctor_id + "Table'>";
							html +="<tr>";
							html +="<td class='doctorSelectTablePicture'>";
							if(data.doctorlist[i].photo == null){
								html +="<img src='resources/images/doctorPicture.png'></td>";
							}else{
								html +="<img src='resources/images/doctorimg/"+data.doctorlist[i].photo+"'></td>";
							}
							html +="<td id='dname'><p class='doctorSelectTablePTag1'>"+data.doctorlist[i].name+"</p>";
							html +="<p class='doctorSelectTablePTag2'>"+data.doctorlist[i].deptname+"</p></td>";
							html +="</tr>";
							html +="<tr>";
							html +="<td class='doctorSelectTable2thTr' colspan='2'>주전공</td>";
							html +="</tr>";
							html +="<tr>";
							html +="<td colspan='2' class='doctorSelectTable3thTr' id='" + data.doctorlist[i].doctor_id + "'>";
							html += "</td>";
							html +="</tr>";
							html +="<tr>";
							html +="<td colspan='2' class='textCenter'>";
							html +="<input class='button' type='button' id='doctorSelectButton' name='" 
								 + data.doctorlist[i].doctor_id + "' value='예약하기'></td>";
							html +="</tr>";
							html +="</table>";
							html +="</td>";	
							html +="</tr>";
							html +="</table>";
							$('.reservation2').append(html);
							$.ajax({
								url : 'doctorProfile2.do',
								data : "doctor_id="+data.doctorlist[i].doctor_id,
								dataType : 'json',
								success : function(data2){
									 for(i = 0; i < data2.doctorlist2.length; i++){
										var list = data2.doctorlist2[i].detail_NAME + " ";
										$("#"+data2.doctorlist2[i].doctor_id).append(list);
									} 
								}
							});
						}	
						$('input:button[id=doctorSelectButton]').click(function() {
							buttonName = $(this).attr('name');
							$('input:hidden[name="doctor_id"]').val(buttonName);
							$.ajax({
								url : 'doctorProfile3.do',
								data : 'doctor_id=' + buttonName,
								dataType : "json",
								success : function(data) {
									var html = "";
									html+="<table class='doctorSelect2'><tr><td>";
									html+="<table class='doctorSelectTable'><tr>";
									html+="<td class='doctorSelectTablePicture'>";

									if(data.doctorlist3[0].photo == null){
										html += "<img src='resources/images/doctorPicture.png'></td>";
									}else{
										html +="<img src='resources/images/doctorimg/"+data.doctorlist3[0].photo+"'></td>";
									}	
									
									html+="<td id='dname'><p class='doctorSelectTablePTag1' id='reserveDoctorName'></p>";
									html+="<p class='doctorSelectTablePTag2' id='doctorDetailName'></p></td></tr>";
									html+="<tr><td class='doctorSelectTable2thTr' colspan='2'>주전공</td>";
									html+="</tr><tr><td colspan='2' class='doctorSelectTable3thTr' id='selectDoctorProfile'></td>";
									html+="</tr></table></td></tr></table>";
									$('.reservation3Div1').append(html);
									var html2 = "";
									html2+="<div id='tabs'>";
									html2+="<ul><li><a href='#tabs-1'>약력</a></li><li><a href='#tabs-2'>인사말</a></li></ul>";
									html2+="<div id='tabs-1'><table class='doctorProfileTable'><tr>";
									html2+="<th class='width25per'>연도</th>";
									html2+="<th class='width75per'>약력</th>";
									html2+="</tr></table>";
									html2+="<table id='doctorHistoryJobTable' class='doctorProfileTable'>";
									html2+="</table>";
									html2+="</div>";
									html2+="<div id='tabs-2'>";
									html2+="<table class='doctorProfileTable'>";
									html2+="<tr>";
									html2+="<td class='doctorGreeting'></td>";
									html2+="</tr>";
									html2+="</table>";
									html2+="</div>";
									html2+="</div>";
									$('.reservation3Div1').append(html2);
									$('#tabs').tabs().css('font-family','"Nanum Gothic", sans-serif').css('font-size','9pt').css('padding','5px').end();
									$('.ui-tabs .ui-tabs-nav .ui-tabs-anchor').css('padding','0px 5px');
									$('.ui-tabs .ui-tabs-panel').css('padding','3px').css('margin-top','3px');
									
									var html3 = "";
									html3+="<div class='datepicker' id='datepicker'></div>";
									html3+="<div id='timepickerCover'>";
									html3+="<img src='resources/images/patient/reserveTimeCover.png'>";
									html3+="</div>";
									html3+="<div id='timepicker'>";
									html3+="</div>";
									$('.reservation3Div2').append(html3);
									
									patient_id = $('#patient_id_temp').val();
									
									var html4 = "";
									html4+="<form action='reserveOk.do' method='post'>";
									html4+="<table class='reserveDateTable'>";
									html4+="<tr>";
									html4+="<td><p id='reserveDateTableP1'>※예약신청</p></td>";
									html4+="</tr>";
									html4+="<tr>";
									html4+="<td><p id='reserveDateTableP2'>다음과 같이 예약을 신청하실 수 있습니다.</p></td>";
									html4+="</tr>";
									html4+="<tr>";
									html4+="<td><p>예약가능시간</p></td>";
									html4+="</tr>";
									html4+="<tr>";
									html4+="<td><input type='text' id='currentDate' name='currentDate' readonly></td>";
									html4+="</tr>";
									html4+="<tr>";
									html4+="<td><p>진료과</p></td>";
									html4+="</tr>";
									html4+="<tr>";
									html4+="<td><p id='reserveResultCategory'>";
									html4+="</p></td>";
									html4+="</tr>";
									html4+="<tr>";
									html4+="<td><p>의료진</p></td>";
									html4+="</tr>";
									html4+="<tr>";
									html4+="<td id='reserveResultDoctorName'></td>";
									html4+="</tr>";
									html4+="<tr>";
									html4+="<td><p>진료실위치</p></td>";
									html4+="</tr>";
									html4+="<tr>";
									html4+="<td><input type='text' value='본원' readonly></td>";
									html4+="</tr>";
									html4+="<tr>";
									html4+="<td><input type='submit' class='button' id='submitBtn' value='인터넷 예약하기  >' style='display:none;'></td>";
									html4+="</tr>";
									html4+="</table>";
									html4+="<input type='hidden' name='schedule_id'>";
									html4+="<input type='hidden' name='patient_id' id='patient_id' value='"+patient_id+"'><br>";
									html4+="<input type='hidden' name='dept_id' id='dept_id' value='"+dept_id+"'>";
									html4+="<input type='hidden' name='doctor_id' id='doctor_id' value='"+buttonName+"'><br>"; 
									html4+="<input type='hidden' name='jupsoo_date' id='enableDate'>";
									html4+="<input type='hidden' name='jupsoo_time' id='enableTime'><br>";
									html4+="<input type='hidden' name='bohum_code_id' id='bohum_code_id' value='BO2'>";
									html4+="<input type='hidden' name='is_night' value='N'><br>";
									html4+="<input type='hidden' name='is_holiday' value='N'>";
									html4+="<input type='hidden' name='jupsoo_code_id' value='jcode01'><br>";
									html4+="</form>";
									$('.reservation3Div3').append(html4);
									
									for(i = 0; i < data.doctorlist3.length; i++){
										$('#reserveDoctorName').append(data.doctorlist3[i].name);
										$('#doctorDetailName').append(data.doctorlist3[i].deptname);
										$('.doctorGreeting').append(data.doctorlist3[i].greeting);
										$('#reserveResultCategory').append("<input type='text' value='"+data.doctorlist3[i].deptname+"' readonly>");	
										$('#reserveResultDoctorName').append("<input type='text' value='"+data.doctorlist3[i].name+"' readonly>");
										$('.reservation2').hide();
										$('.reservation3').fadeIn(1000);
									}
									
									$.ajax({
										url : 'doctorProfile2.do',
										data : "doctor_id="+buttonName,
										dataType : 'json',
										success : function(data){
											
											for(i = 0; i < data.doctorlist2.length; i++){
												var list = data.doctorlist2[i].detail_NAME + " ";
												$("#selectDoctorProfile").append(list);
											};
											
											$.ajax({
												url : 'doctorHistory.do',
												data : "doctor_id="+buttonName,
												dataType : 'json',
												success : function(data){
													
													for(i = 0; i < data.doctorHistory.length; i++){
														html = "";
														html += "<tr>";
														html += "<td style='width:25%;'>" + data.doctorHistory[i].start_year.substring(0,4) 
															 + "~" + data.doctorHistory[i].end_year.substring(0,4)+"</td>";
														html += "<td style='width:75%;'>" + data.doctorHistory[i].content+"</td>";
														html += "</tr>";
														$('#doctorHistoryJobTable').append(html);
													}
													
													
													$.ajax({
														url : 'doctorScheduleDate.do',
														data : "doctor_id="+buttonName,
														dataType : 'json',
														success : function(data){
															var enableDay=[];
															enableDay = data.enableDay;
														  		function selectableDays(date) {
														  			y = date.getFullYear(); // 년,월,일로 값을 받는다.
														            m = date.getMonth() + 1;
														            d = date.getDate();	
														            if (m < 10) m = "0" + m; 
													                if (d < 10) d = "0" + d;	
													                dummy = y + "-" + m + "-" + d;
														  			if ($.inArray(dummy, enableDay) > -1) {
														  				return [ true, "" ];
														  			}
														  			return [ false, "" ];
														  		}
														  		$(function() {
														  			$("#datepicker").datepicker(
														  					{
														  						showMonthAfterYear : true,
														  						minDate : '1d',
														  						yearSuffix : '년',
														  						monthNames : [ '1월', '2월', '3월', '4월', '5월', '6월',
														  								'7월', '8월', '9월', '10월', '11월', '12월' ],
														  						monthNamesShort : [ '1월', '2월', '3월', '4월', '5월', '6월',
														  								'7월', '8월', '9월', '10월', '11월', '12월' ],
														  						dayNamesMin : [ '일', '월', '화', '수', '목', '금', '토' ],
														  						dateFormat : 'yy-mm-dd',
														  						/* dateFormat : 'd-m-yy', */ 
														  						beforeShowDay : selectableDays,
														  						onSelect : function(dateText, datePicker) {
														  							$('#timepickerCover').hide();
														  							$('#timepicker').show();
														  							scheduleDateDisplay = dateText.replace('-','년 ').replace('-','월 ')+'일 ';
														  							$('input:hidden[name=jupsoo_date]').val(dateText);
														  							
														  							
														  							$.ajax({
																						url : 'doctorScheduleTime.do',
																						data : "doctor_id="+buttonName+"&sdate="+dateText,
																						dataType : 'json',
																						success : function(data){
																							var enableTime=[];
																							
																							$('#timepicker').empty();
																							
																							for(i = 0; i < data.enableTime.length; i++){
																								html="";
																								html+="<input type='button' name='enableTimeButton' class='button' value='"+data.enableTime[i]+"'>";                   
																								$('#timepicker').append(html);
																							}
																							
																							$('input:button[name="enableTimeButton"]').click(function() {
																								$('#submitBtn').show();
																								enableTimeValue = $(this).attr('value');
																								scheduleTimeDisplay = enableTimeValue.replace(':','시 ')+"분";
																								$('input:hidden[name="jupsoo_time"]').val(enableTimeValue);
																								$("#currentDate").val(scheduleDateDisplay + scheduleTimeDisplay);
																								currentDate = $('#currentDate').val();
																								if(currentDate != ""){
																									$('#submitBtn').show();
																								}
																								
																								$.ajax({
																									url : 'doctorScheduleId.do',
																									data : "doctor_id="+buttonName+"&sdate="+dateText+"&res_time=" + enableTimeValue ,
																									dataType : 'json',
																									success : function(data){
																										schedule_id = data.doctorScheduleId[0].schedule_id;
																										$('input:hidden[name=schedule_id]').val(schedule_id);
																									}
																								});
																							});
																						}
																					});
														  						}
														  					});
														  				});
																	}
																});
												
												}
											
											});
											
										}
										
									});
									
								}
									
							});
								
						});	
							
					}						
				});				
			});				
		});